/**
 * Created by xiongzhao
 */

/**
 * @param page size dataList
 * @returns 分页数据
 */
export function paged(page, size, dataList) {
  let total = dataList.length

  let len = total / size

  let totalPages = len - parseInt(len) > 0 ? parseInt(len) + 1 : len

  let newDataList = dataList.slice(page * size, (page + 1) * size)

  return {
    list: newDataList,
    total,
    page,
    size
  }
}




/**
 * 
 * 
 * @param fieldQuery  {}
 * @returns 字段查询
 * ///////Object.keys遍历属性/////////
    let obj = {a:1, b:2,c:3};
    Object.keys(obj); //["a", "b", "c"]返回对象属性数组
    /////////Object.keys遍历value/////////
    let obj = {a:1,b:2,c:function(){}};
    Object.values(obj); //[1, 2, f]返回对象属性value值数组
 */
export function dataByField(fieldQuery, dataList) {


  let dataListByField = [] // 需要返回的新列表数据

  let isFieldQueryAllNull = true
  for (let l = 0; l < Object.values(fieldQuery).length; l++) {
    console.log(Object.values(fieldQuery)[l])
    if(Object.values(fieldQuery)[l] != null ||Object.values(fieldQuery)[l] !=''){
      isFieldQueryAllNull = false
    }
  }

  if(isFieldQueryAllNull){  //如果字段参数都为空  不作过滤直接返回数据
    return dataList
  }

  for (let i = 0; i < dataList.length; i++) {

    let fieldValues = Object.values(dataList[i])
    let fieldQueryValues = Object.values(fieldQuery)
    for (let j = 0; j < fieldValues.length; j++) {
      let currentFieldValue = fieldValues[j]

      for (let k = 0; k < fieldQueryValues.length; k++) {
        let currentFieldQueryValue = fieldQueryValues[k]
        if (currentFieldQueryValue === currentFieldValue) {
          dataListByField.push(dataList[i])
        }
      }

    }

  }


  return dataListByField
}
